package com.ruoyi.system.service.impl.usable;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.TotalResultMap;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.usable.VsDramasUsable;
import com.ruoyi.system.domain.video.VsLang;
import com.ruoyi.system.mapper.usable.VsDramasUsableMapper;
import com.ruoyi.system.mapper.video.VsLangMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.usable.VsDramasUsableOrderMapper;
import com.ruoyi.system.domain.usable.VsDramasUsableOrder;
import com.ruoyi.system.service.usable.IVsDramasUsableOrderService;

/**
 * 积分订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class VsDramasUsableOrderServiceImpl implements IVsDramasUsableOrderService 
{
    @Autowired
    private VsDramasUsableOrderMapper vsDramasUsableOrderMapper;

    @Autowired
    private VsDramasUsableMapper vsDramasUsableMapper;

    @Autowired
    private VsLangMapper vsLangMapper;

    /**
     * 查询积分订单
     * 
     * @param id 积分订单主键
     * @return 积分订单
     */
    @Override
    public VsDramasUsableOrder selectVsDramasUsableOrderById(Long id)
    {
        return vsDramasUsableOrderMapper.selectVsDramasUsableOrderById(id);
    }

    /**
     * 查询积分订单列表
     * 
     * @param vsDramasUsableOrder 积分订单
     * @return 积分订单
     */
    @Override
    public List<VsDramasUsableOrder> selectVsDramasUsableOrderList(VsDramasUsableOrder vsDramasUsableOrder)
    {
        return vsDramasUsableOrderMapper.selectVsDramasUsableOrderList(vsDramasUsableOrder);
    }

    /**
     * 新增积分订单
     * 
     * @param vsDramasUsableOrder 积分订单
     * @return 结果
     */
    @Override
    public String insertVsDramasUsableOrder(VsDramasUsableOrder vsDramasUsableOrder)
    {
        VsDramasUsable usable = vsDramasUsableMapper.selectVsDramasUsableById(Math.toIntExact(vsDramasUsableOrder.getId()));
        VsDramasUsableOrder order = new VsDramasUsableOrder();
        order.setUsableId(usable.getId());
        String s = "U" + System.currentTimeMillis() + IdUtils.fastSimpleUUID();
        order.setOrderSn(s);
        order.setLangId(vsDramasUsableOrder.getLangId());
        Long id = SecurityUtils.getLoginUser().getVsUser().getId();
        order.setUserId(Math.toIntExact(id));
        order.setUsable(usable.getUsable());
        order.setStatus(0);
        order.setTotalFee(usable.getPrice());
        order.setPayFee(BigDecimal.valueOf(0));
        order.setCreatetime(System.currentTimeMillis());
        order.setUpdatetime(System.currentTimeMillis());
        int i = vsDramasUsableOrderMapper.insertVsDramasUsableOrder(order);
        if (i > 0) {
            return s;
        }
        return "err";
    }

    /**
     * 修改积分订单
     * 
     * @param vsDramasUsableOrder 积分订单
     * @return 结果
     */
    @Override
    public int updateVsDramasUsableOrder(VsDramasUsableOrder vsDramasUsableOrder)
    {
        vsDramasUsableOrder.setUpdatetime(System.currentTimeMillis());
        return vsDramasUsableOrderMapper.updateVsDramasUsableOrder(vsDramasUsableOrder);
    }

    /**
     * 批量删除积分订单
     * 
     * @param ids 需要删除的积分订单主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasUsableOrderByIds(Long[] ids)
    {
        return vsDramasUsableOrderMapper.deleteVsDramasUsableOrderByIds(ids);
    }

    /**
     * 删除积分订单信息
     * 
     * @param id 积分订单主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasUsableOrderById(Long id)
    {
        return vsDramasUsableOrderMapper.deleteVsDramasUsableOrderById(id);
    }

    @Override
    public TotalResultMap selectCount(VsDramasUsableOrder vsDramasUsableOrder) {
        return vsDramasUsableOrderMapper.selectCount(vsDramasUsableOrder);
    }
}
